package com.zzx.controller;

import com.zzx.model.entity.PageResult;
import com.zzx.model.entity.Result;
import com.zzx.model.entity.StatusCode;
import com.zzx.model.pojo.Fruit;
import com.zzx.service.FruitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zzx.utils.FormatUtil;

@Api(tags = "果子api", description = "果子api", basePath = "/fruit")
@RestController
@RequestMapping("/fruit")
public class FruitController {

    /**
     * 处理labelList
     * @return
     */
    private List<Map<String, String>> handleLabelList() {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> obj1 = new HashMap<>();
        obj1.put("label", "地址");
        obj1.put("prop", "address");
        obj1.put("type", "address");
        list.add(obj1);

        Map<String, String> obj2 = new HashMap<>();
        obj2.put("label", "联系人");
        obj2.put("prop", "contacts");
        obj2.put("type", "text");
        list.add(obj2);

        Map<String, String> obj3 = new HashMap<>();
        obj3.put("label", "电话");
        obj3.put("prop", "number");
        obj3.put("type", "phone");
        list.add(obj3);

        Map<String, String> map4 = new HashMap<>();
        map4.put("label", "能否提供快递服务");
        map4.put("prop", "isExpress");
        map4.put("type", "text");
        list.add(map4);

        Map<String, String> map5 = new HashMap<>();
        map5.put("label", "是否支持自驾前往");
        map5.put("prop", "isSelfDriving");
        map5.put("type", "text");
        list.add(map5);

        Map<String, String> map6 = new HashMap<>();
        map6.put("label", "供应品种");
        map6.put("prop", "variety");
        map6.put("type", "text");
        list.add(map6);

        Map<String, String> map7 = new HashMap<>();
        map7.put("label", "种植面积（亩）");
        map7.put("prop", "area");
        map7.put("type", "col3");
        list.add(map7);

        Map<String, String> map8 = new HashMap<>();
        map8.put("label", "预计产量（吨）");
        map8.put("prop", "yield");
        map8.put("type", "col3");
        list.add(map8);

        Map<String, String> map9 = new HashMap<>();
        map9.put("label", "上市时间");
        map9.put("prop", "publishTime");
        map9.put("type", "col3");
        list.add(map9);

        return list;
    }

    @Autowired
    private FruitService fruitService;

    @Autowired
    private FormatUtil formatUtil;



    /**
     * 整合labelList、dataList
     * @return
     */
    public Map<String, Object> handleRes() {
        Map<String, Object> labelListMap = new HashMap<>();
        // labelList
        List<Map<String, String>> labelList = handleLabelList();
        labelListMap.put("labelList", labelList);
        // dataList
        List<Fruit> dataList = fruitService.findAllFruit();
        System.out.println(dataList);
        labelListMap.put("dataList", dataList);
        return labelListMap;
    }

    /**
     * 查询全部果子
     * @return
     */
    @ApiOperation(value = "果园api", notes = "列表")
    @GetMapping("/getFruitList")
    public Result getFruitList() {
        return Result.create(StatusCode.OK, "查询成功", handleRes());
    }

    /**
     * 新增果园
     * @param fruit
     * @return
     */
    @ApiOperation(value = "果园api", notes = "新增")
    @PostMapping("/addFruit")
    public Result addFruit(Fruit fruit) {
        System.out.println(fruit);
//        if(!formatUtil.checkStringNull(name) || !formatUtil.checkStringNull(address)) {
//            return Result.create(StatusCode.ERROR, "参数错误");
//        }
        try {
            fruitService.addFruit(fruit);
            return Result.create(StatusCode.OK, "新增成功");
        } catch (RuntimeException e) {
            System.out.println("报错了");
            System.out.println(e);
            return Result.create(StatusCode.ERROR, "新增失败");
        }
    }

    /**
     * 编辑
     * @param fruit
     * @return
     */
    @ApiOperation(value = "果园api", notes = "编辑")
    @PostMapping("/editFruit")
    public Result editFruit(Fruit fruit) {
        try {
            fruitService.editFruit(fruit);
            return Result.create(StatusCode.OK, "编辑成功");
        } catch (RuntimeException e) {
            System.out.println("报错了");
            System.out.println(e);
            return Result.create(StatusCode.ERROR, "编辑失败");
        }
    }
    /**
     * 删除
     * @param
     * @return
     */
    @ApiOperation(value = "果园api", notes = "删除")
    @PostMapping("/delFruit")
    public Result delFruit(Integer id) {
        try {
            fruitService.delFruit(id);
            return Result.create(StatusCode.OK, "删除成功");
        } catch (RuntimeException e) {
            System.out.println("报错了");
            System.out.println(e);
            return Result.create(StatusCode.ERROR, "删除失败");
        }
    }



}
